package com.zzw.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzw.common.result.Result;
import com.zzw.model.system.SysRole;
import com.zzw.vo.system.AssginRoleVo;
import com.zzw.vo.system.SysRoleQueryVo;
import com.zzw.system.annotation.Log;
import com.zzw.system.enums.BusinessType;
import com.zzw.system.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "角色管理")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;


    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation(value = "获取全部角色列表")
    @GetMapping("findAll")
    public Result<List<SysRole>> findAll() {
        return Result.ok(sysRoleService.list());

    }
    @Log(title = "角色管理", businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation(value = "删除角色")
    @DeleteMapping("removeRole/{id}")
    public Result removeRole(@PathVariable Long id) {
        if (sysRoleService.removeById(id)) {
            return Result.ok();
        }
        return Result.fail();

    }
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation(value = "获取分页列表")
    @GetMapping("/{page}/{limit}")
    public Result findPageQueryRole(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "roleQueryVo", value = "查询对象", required = false)
            SysRoleQueryVo roleQueryVo) {
        Page<SysRole> pageParam = new Page<>(page, limit);
        IPage<SysRole> pageModel = sysRoleService.selectPage(pageParam, roleQueryVo);
        return Result.ok(pageModel);
    }
    @Log(title = "角色管理", businessType = BusinessType.INSERT)
    @PreAuthorize("hasAuthority('bnt.sysRole.add')")
    @ApiOperation("添加角色")
    @PostMapping("saveRole")
    public Result saveRole(@RequestBody SysRole sysRole) {
        if (sysRoleService.save(sysRole)) {
            return Result.ok();
        }
        return Result.fail();
    }

    //    修改角色的步骤
//    1.根据id查找角色
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("根据id查找角色")
    @PostMapping("fidRoleById/{id}")
    public Result<SysRole> fidRoleById(@PathVariable Long id) {
        return Result.ok(sysRoleService.getById(id));
    }

    //    2.最终修改
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PreAuthorize("hasAuthority('bnt.sysRole.update')")
    @ApiOperation("修改角色")
    @PutMapping("updateRole")
    public Result updateRole(@RequestBody SysRole sysRole) {
        if (sysRoleService.updateById(sysRole)) {

            return Result.ok();
        }
        return Result.fail();
    }
    //    @RequestBody传入为json格式
    //    json格式中的数组对于java的list集合
    @Log(title = "角色管理", businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation("批量删除")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> ids) {
        if (sysRoleService.removeByIds(ids)) {

            return Result.ok();
        }
        return Result.fail();
    }

    @ApiOperation("根据用户获取角色数据")
    @GetMapping("toAssign/{userId}")
    public Result toAssign(@PathVariable Long userId){
        Map<String,Object> roleMap =sysRoleService.getRolesById(userId);
        return Result.ok(roleMap);
    }

    @ApiOperation(value = "根据用户分配角色")
    @PostMapping("doAssign")
    public Result doAssign(@RequestBody AssginRoleVo assginRoleVo){
        sysRoleService.doAssign(assginRoleVo);
        return Result.ok();
    }
}